﻿/********************************************************************************************
 * 文件名称:	List_Edit.cs
 * 设计人员:	严向晖(yanxianghui@gmail.com)
 * 设计时间:	2009-04-10
 * 功能描述:	添加豆列
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		    修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Shanfree.Framework.Utility;
using BLL = Shanfree.SharpMovie.BLL;
using Shanfree.SharpMovie.Model;
using System.Web.UI.WebControls;
using System.Web.UI;

public partial class List_Edit : BLL.BasePage
{
    /// <summary>
    /// 列表的主键
    /// </summary>
    public string ListID
    {
        get
        {
            return GetViewState("ListID");
        }
        set
        {
            ViewState["ListID"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                BindMenuList();
                string listID = GetRequest("id");
                if (string.IsNullOrEmpty(listID))
                {
                    // 新增

                }
                else
                {
                    // 编辑
                    this.ListID = listID;
                    ListInfo lInfo = new BLL.List().GetByID(listID);
                    if (lInfo == null)
                    {
                        ShowMsg("指定收藏分类不存在。");
                    }
                    tbListName.Text = lInfo.Name;
                    tbListRemark.Text = lInfo.Remark;
                    lbAmount.Text = lInfo.Amount.ToString();
                    tbTotal.Text = lInfo.Total.ToString();
                    lbCreateTime.Text = lInfo.CreateTime.ToShortDateString();
                    dtMenu.Name = lInfo.CatalogName;
                    BindList();
                }
            }
        }
        catch (Exception ex)
        {
            ShowMsg(ex.Message);
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }
    private void BindMenuList()
    {
        TreeNodeInfo<CatalogInfo> list = new BLL.Catalog().GetTree(CatalogInfo.DEFAULT_PARENT_ID);
        dtMenu.DataSource = list;
        dtMenu.DataBind();
    }
    private void BindList()
    {
        string listID = this.ListID;
        int pageSize = pGrid.PageSize;
        int pageIndex = pGrid.CurrentPageIndex;
        IList<ListItemInfo> itemList = new BLL.List().GetItemList(listID, pageSize, pageIndex);
        ListItemInfo newInfo = new ListItemInfo();
        newInfo.IMDB_ID = "tt";
        itemList.Add(newInfo);
        CustomList<ListItemInfo> cList = itemList as CustomList<ListItemInfo>;
        pGrid.TotalAmout = cList.TotalAmout;
        gvList.DataSource = cList;
        gvList.DataBind();
    }

    protected void pGrid_OnClick(object sender, EventArgs e)
    {
        try
        {
            BindList();
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnAdd = (Button)e.Row.FindControl("btnAdd");
            Button btnEdit = (Button)e.Row.FindControl("btnEdit");
            Button btnDel = (Button)e.Row.FindControl("btnDel");
            TextBox tbIMDB = (TextBox)e.Row.FindControl("tbIMDB");
            Label Lable1 = (Label)e.Row.FindControl("Lable1");
            TextBox tbSortIndex = (TextBox)e.Row.FindControl("tbSortIndex");

            if (tbIMDB == null || tbIMDB.Text == "tt")
            {
                btnAdd.Visible = true;
                btnEdit.Visible = false;
                btnDel.Visible = false;
                Lable1.Visible = true;
                tbSortIndex.Visible = false;
            }
            else
            {
                btnAdd.Visible = false;
                btnEdit.Visible = true;
                btnDel.Visible = true;
            }
        }
    }

    protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string itemID = e.CommandArgument.ToString();
            if (e.CommandName == "btnDel")
            {
                new BLL.ListItem().Delete(itemID);
            }
            else
            {
                #region 控件
                Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl，保持统一性并便于后续操作，我们这里直接转化为控件基类 Control
                GridViewRow gvr = cmdControl.NamingContainer as GridViewRow;
                TextBox tbSortIndex = (TextBox)gvr.FindControl("tbSortIndex");
                TextBox tbIMDB = (TextBox)gvr.FindControl("tbIMDB");
                TextBox tbRemark = (TextBox)gvr.FindControl("tbRemark");
                int sort = Convert.ToInt32(tbSortIndex.Text);
                string imdb = tbIMDB.Text;
                string remark = tbRemark.Text;
                #endregion

                if (e.CommandName == "btnEdit")
                {
                    new BLL.ListItem().Update(itemID, remark, sort);
                }

                if (e.CommandName == "btnAdd")
                {
                    string movieID = new BLL.Movie().Import(imdb, this.CurrentUser.ID);
                    new BLL.ListItem().Add(this.ListID, movieID, remark);
                }
            }
            ShowMsg("操作成功！");
            BindList();
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.其他);
        }
    }
    /// <summary>
    /// 保存或新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        // 我的收藏中默认添加一影片
        string name = tbListName.Text;
        string remark = tbListRemark.Text;
        string total = tbTotal.Text;
        string catalogID = dtMenu.NodeID;
        if (string.IsNullOrEmpty(this.ListID))
        {
            this.ListID = new BLL.List().Add(name, remark, total, catalogID, this.CurrentUser.ID);
        }
        else
        {
            new BLL.List().Update(this.ListID, name, remark, total,catalogID);
        }
    }
}
